package com.zhixing.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhixing.common.domain.vo.DriverInfoVO;
import com.zhixing.common.exception.ZhixingException;
import com.zhixing.user.domain.dto.AuthInfoDTO;
import com.zhixing.user.domain.entity.Driver;
import com.zhixing.user.domain.entity.User;
import com.zhixing.user.domain.enums.AuthStatus;
import com.zhixing.user.domain.vo.AuthStatusVO;
import com.zhixing.user.mapper.DriverMapper;
import com.zhixing.user.mapper.UserMapper;
import com.zhixing.user.service.DriverService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import static com.zhixing.common.domain.enums.HttpStatus.IDENTITY_NOT_CONSISTENT;

@Service
@RequiredArgsConstructor
public class DriverServiceImpl implements DriverService {

    private final DriverMapper driverMapper;
    private final UserMapper userMapper;

    @Override
    public AuthStatusVO getDriverAuthStatus(Integer userId) {
        // 查询是否认证
        LambdaQueryWrapper<Driver> lqw = new LambdaQueryWrapper<Driver>().eq(Driver::getUserId, userId);
        Driver driver = driverMapper.selectOne(lqw);
        if (ObjectUtil.isEmpty(driver)) return new AuthStatusVO(AuthStatus.NO_AUTH, null);

        // 判断证件是否过期
        if (driver.getIdCardExpireDate().isBefore(LocalDate.now()) ||
                driver.getDriverLicenseExpireDate().isBefore(LocalDate.now())) {
            return new AuthStatusVO(AuthStatus.AUTH_EXPIRE, null);
        }
        return new AuthStatusVO(AuthStatus.ALREADY_AUTH, driver.getId());
    }

    @Override
    public void createOrUpdateDriverAuthInfo(Integer userId, AuthInfoDTO authInfoDTO) {
        // 查询是否认证过
        LambdaQueryWrapper<Driver> lqwDriver = new LambdaQueryWrapper<Driver>().eq(Driver::getUserId, userId);
        Driver driver = driverMapper.selectOne(lqwDriver);
        if (ObjectUtil.isEmpty(driver)) {
            // 创建司机认证信息
            LambdaQueryWrapper<User> lqwUser = new LambdaQueryWrapper<User>().select(User::getPhone).eq(User::getId, userId);
            String phone = (String) userMapper.selectObjs(lqwUser).get(0);
            driver = Driver.builder().userId(userId).phone(phone).build();
            BeanUtil.copyProperties(authInfoDTO, driver);
            driverMapper.insert(driver);
        }
        else {
            // 更新司机认证信息
            if (!authInfoDTO.getIdNumber().equals(driver.getIdNumber())) throw new ZhixingException(IDENTITY_NOT_CONSISTENT);
            BeanUtil.copyProperties(authInfoDTO, driver);
            driverMapper.updateById(driver);
        }
    }

    @Override
    public DriverInfoVO getDriverInfo(Integer driverId) {
        return driverMapper.getDriverInfo(driverId);
    }

    @Override
    public Integer getUserIdByDriverId(Integer driverId) {
        return driverMapper.getUserIdByDriverId(driverId);
    }
}
